﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using loveMotorcade.web.Models;

namespace loveMotorcade.web.Areas.manage.Controllers
{
    using Classes;
    public class carClassController : Controller
    {
        ModelContainer db = new ModelContainer();
        //
        // GET: /manage/carClass/

        public ActionResult Index(int pageIndex=1 ,int pageSize=16)
        {
            DateTime ndt = DateTime.Now;
            ViewBag.count = db.carClass.Count();
            ViewData.Model = db.carClass.Select<carClass, carClassExt>(d => new carClassExt() { basc = d, orderAll = d.order.Count(), orderAble = d.order.Where(dd => dd.starTime < ndt).Count() }).OrderBy(d => d.basc.name).Skip((pageIndex - 1) * pageSize).Take(pageSize); 
            return View();
        }

        //
        // GET: /manage/carClass/Details/5

        public ActionResult Edit(int id=0)
        {
            carClass cc;
            cc = db.carClass.Where(d => d.ID == id).FirstOrDefault() ?? new carClass();
            ViewData.Model = cc;

            return View();
        }

        public JsonResult uploadPic(int id) {
            carClass cc = db.carClass.Where(d => d.ID == id).FirstOrDefault();
            if(cc==null){return AjaxResultForSWFUpload.jsonError();}


            HttpFileCollectionBase files = Request.Files;

            myUpload mu = new myUpload();
            mu.myDirectory = "carClassPic";
            mu.imageResize = "600*800";
            string[] urls = mu.imageSave(files,true);

            cc.pics = String.Join(",",cc.pics.tryToArray(",").Concat<string>(urls));
            db.SaveChanges();

            return AjaxResultForSWFUpload.jsonOk(String.Join(",", urls));
        }
        public JsonResult delPic(int id,string filename) {
            carClass cc = db.carClass.Where(d => d.ID == id).FirstOrDefault();
            if (cc == null) { return AjaxResultForSWFUpload.jsonError(); }

            var piclist = cc.pics.tryToArray(",").ToList();
            List<string> newPiclist = new List<string>();
            foreach (var p in piclist) {
                
                if (p != filename) {
                    newPiclist.Add(p);
                }
            }
            cc.pics = String.Join(",", newPiclist);
            db.SaveChanges();
            return AjaxResultForSWFUpload.jsonOk(myUpload.delImage("/Content/upload/carClassPic/" + filename).ToString());
        }

        //
        // POST: /manage/carClass/Create

        [HttpPost]
        public ActionResult Edit(int id=0,FormCollection collection=null)
        {
            if (id == 0)
            {
                carClass cc = new carClass();
                TryUpdateModel<carClass>(cc);
                try
                {
                    // TODO: Add insert logic here


                    db.AddTocarClass(cc);
                    db.SaveChanges();

                    return RedirectToAction("Index");
                }
                catch
                {
                    ViewData.Model = cc;
                    return View();
                }
            }
            else {
                carClass cc = db.carClass.Where(d => d.ID == id).FirstOrDefault();
                if (cc != null)
                {
                    TryUpdateModel<carClass>(cc);
                    try
                    {

                        db.SaveChanges();

                        return RedirectToAction("Index");
                    }
                    catch
                    {
                        ViewData.Model = cc;
                        return View();
                    }
                }
                return View();
            }
        }


        //
        // POST: /manage/carClass/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                carClass cc = db.carClass.Where(d => d.ID == id).FirstOrDefault();

                db.carClass.DeleteObject(cc);
                db.SaveChanges();

                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
